package me.shurufa.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
import me.shurufa.R;
import me.shurufa.holder.MessageViewHolder;
import me.shurufa.model.Message;

/**
 * 会话消息列表
 *
 * @FileName: me.shurufa.adapter.ChatMessageAdapter.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-01-26 16:20
 */
public class ChatMessageAdapter extends BaseAdapter {

    List<Message> dataList;
    Context mContext;


    public ChatMessageAdapter(Context context, List<Message> dataList) {
        this.dataList = dataList;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MessageViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_msg,
                    parent, false);
            holder = new MessageViewHolder(convertView, mContext);
            convertView.setTag(holder);

        } else {
            holder = (MessageViewHolder) convertView.getTag();
        }
        Message message = dataList.get(position);
        holder.bind(message);
        return convertView;
    }

    public void append(Message[] items) {
        int pos = dataList.size();
        for (Message item : items) {
            if (!dataList.contains(item))
                dataList.add(item);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        int size = dataList.size();
        dataList.clear();
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (dataList.size() > 0) {
            dataList.remove(position);
            notifyDataSetChanged();
        }
    }
}
